package com.wdhcr.controller;

import com.wdhcr.entity.Blog;
import com.wdhcr.entity.Tag;
import com.wdhcr.enums.ResultCode;
import com.wdhcr.service.BlogService;
import com.wdhcr.service.TagService;
import com.wdhcr.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
 * @description: 前端标签页的请求数据的控制层
 * @author: wdhcr
 * @dateTime: 2020/3/24 22:35
 */
@RestController

public class ShowTagController {



    @Autowired
    private BlogService blogService;
    @Autowired
    private TagService tagService;

    /*
     * @description: 标签页的标签列表的请求接口
     * @params: []
     * @return: com.wdhcr.utils.ResponseResult
     * @author: wdhcr
     * @dateTime: 2020/3/24 21:30
     */
    @CrossOrigin
    @GetMapping("showTagList")
    public ResponseResult showTagList(){

        List<Tag> tags = tagService.listTag();
        if (tags !=null){
            return new ResponseResult(ResultCode.SUCCESS.getCode(),"获取成功!",tags);
        }
        return new ResponseResult(ResultCode.SUCCESS.getCode(),"获取失败!",null);
    }

    @CrossOrigin
    @PostMapping("showBlogByTag/{page}/{size}")
    public ResponseResult showBlogByTag(@PathVariable("page") Integer page, @PathVariable("size") Integer size, @RequestBody Tag tag){
        //根据更新时间倒叙排列
        Sort update_time = Sort.by(Sort.Direction.DESC,"updateTime");
        PageRequest pageRequest = PageRequest.of(page, size,update_time);
        Page<Blog> blogByTag = blogService.findBlogByTag(pageRequest, tag);
        if (blogByTag !=null){
            return new ResponseResult(ResultCode.SUCCESS.getCode(),"获取成功!",blogByTag);
        }
        return new ResponseResult(ResultCode.SUCCESS.getCode(),"获取成功!",null);
    }
}
